<template>
  <div>
    <!-- 排班管理 -->
    <el-card class="card">
      <div class="userInfo_box">
        <img
          :src="
            elderlyData.photo
              ? imgUrl + elderlyData.photo
              : 'http://123.57.237.81:8080/caresystem/upload/2024/11/26/20241126195954213.jpg'
          "
          style="width: 50px; height: 50px"
          alt=""
        />
        <div class="content">
          <div class="content_item">
            <span class="name">老人姓名：</span>
            <span>{{ elderlyData.name }}</span>
          </div>
          <div class="content_item">
            <span class="name">身份证/护照号：</span>
            <span>{{ elderlyData.idCard }}</span>
          </div>
          <div class="content_item">
            <span class="name">床位号：</span>
            <span>{{ elderlyData.begName }}</span>
          </div>
        </div>
      </div>
    </el-card>
    <el-card class="card">
      <div>排班管理</div>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="type" width="160" />
        <el-table-column v-for="(item, index) in weeks" :prop="item" :label="item">
          <template #default="scope">
            <div>
              <div
                v-for="(colItem, colIndex) in scope.row[item]"
                style="display: flex; align-items: center"
              >
                <div class="headImg" @click="select(scope.row.type, item)">
                  <img
                    :src="imgUrl + colItem.staffPhoto"
                    style="width: 30px; height: 30px; border-radius: 50%"
                    alt=""
                  />
                  <div class="cha" @click.stop="del(colItem.id, scope.$index, colIndex)">×</div>
                </div>
                <div style="margin-left: 6px">{{ colItem.staffName }}</div>
              </div>
              <div v-if="!scope.row[item].length" @click="select(scope.row.type, item)">
                +请选择
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <selectStaff v-if="dialogVisible" @close="closeHandle" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import selectStaff from "@/components/market/elderly/selectStaff.vue";
import { addSchedule, delSchedule, getScheduleList } from "@/api/market/elderly/elderly";
import { AddScheduleParams, ScheduleData } from "@/api/market/elderly/type";
import { getElderlyModel } from "@/api/market/elderly/elderly";
import { ElderlyData } from "./Records.vue";

const route = useRoute();
const router = useRouter();

//dialog显示
const dialogVisible = ref(false);

// 图片根路径
const imgUrl = ref(import.meta.env.VITE_IMG_URL);

const list = ref<Record<string, string | ScheduleData[]>[]>([]);

const weeks = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
//获取老人排班列表
const getScheduleData = async (id: number) => {
  list.value = [];
  let dayData: Record<string, string | ScheduleData[]> = { type: "白班" };
  let nighyData: Record<string, string | ScheduleData[]> = { type: "夜班" };

  let res = await getScheduleList(id);
  console.log(566, res.list);

  weeks.forEach((item: string) => {
    dayData[item] = res.list.filter(
      (sonItem: ScheduleData) => sonItem.day == "白班" && sonItem.week == item
    );
    nighyData[item] = res.list.filter(
      (sonItem: ScheduleData) => sonItem.day == "夜班" && sonItem.week == item
    );
  });

  list.value.push(dayData);
  list.value.push(nighyData);
};

//添加参数
const scheduleParams = reactive<AddScheduleParams>({
  elderlyId: Number(route.query.id),
  day: "",
  week: "",
  staffId: 0,
});
//选择
const select = (data: string, item: string) => {
  dialogVisible.value = true;
  //类型
  scheduleParams.day = data;
  //周几
  scheduleParams.week = item;
};

//关闭dialog
const closeHandle = (data: any) => {
  dialogVisible.value = false;
  if (data) {
    scheduleParams.staffId = data.id;
    //调用添加
    add();
  }
};

//老人信息
const elderlyData = reactive<ElderlyData>({});

//获取老人详情
const getElderlyDetail = async (id: number) => {
  let res = await getElderlyModel(id);
  Object.assign(elderlyData, res);
};

//添加
const add = async () => {
  await addSchedule(scheduleParams);
  // console.log("8777res",res);
  //刷新
  getScheduleData(Number(route.query.id));
};

//删除
const del = async (id: number, index: number, colIndex: number) => {
  await delSchedule(id);
  ElMessage({
    message: "删除成功",
    type: "success",
  });
  //刷新
  getScheduleData(Number(route.query.id));
};

onMounted(() => {
  if (route.query.id) {
    getScheduleData(Number(route.query.id));
    getElderlyDetail(Number(route.query.id));
  }
});
</script>

<style scoped>
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;

  .userInfo_box {
    display: flex;

    img {
      margin-top: 10px;
    }

    .content {
      margin-left: 55px;

      .content_item {
        margin-bottom: 10px;

        .name {
          display: inline-block;
          width: 160px;
        }
      }
    }
  }

  .headImg {
    position: relative;
    display: flex;
    align-items: center;
    margin-top: 2px;

    .cha {
      position: absolute;
      top: -2px;
      right: -8px;
      z-index: 100;
      width: 16px;
      height: 16px;
      font-size: 16px;
      line-height: 16px;
      text-align: center;
      background-color: #ccc;
      border-radius: 50%;
    }
  }
}
</style>
